package io.minio.spring.boot.autoconfigure;

import io.minio.MinioClient;
import io.minio.spring.boot.autoconfigure.properties.MinioClientProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

/**
 * @author CSY
 * @date 2025 - 09 - 15
 */
@Configuration

@ConditionalOnClass(MinioClient.class)

@ConfigurationPropertiesScan("io.minio.spring.boot.autoconfigure.properties")
public class MinioClientAutoConfigure {
    @Autowired
    private MinioClientProperties properties;

    @Bean
    @ConditionalOnMissingBean(MinioClient.class)
    public MinioClient minioClient() {
        MinioClient client = MinioClient.builder()
                .endpoint(properties.getHost())
                .credentials(properties.getAccount(), properties.getPassword())
                .build();
        return client;
    }

    @Bean
    @ConditionalOnMissingBean(MinioOptions.class)
    public MinioOptions minioOptions() {
        return new MinioOptions(minioClient(),properties.getBucketName());
    }

}
